package com.powroznik.tracker.engine.util;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;

import org.codehaus.jettison.json.JSONObject;

import com.powroznik.tracker.engine.data.Account;
import com.powroznik.tracker.engine.data.Device;
import com.powroznik.tracker.engine.data.DeviceConfiguration;
import com.powroznik.tracker.engine.data.Location;
import com.powroznik.tracker.engine.data.User;

public class ConversionUtils {
	public static boolean equals(Object o1, Object o2) {
		if (o1 == null && o2 == null) {
			return true;
		}
		if (o1 == null || o2 == null) {
			return false;
		}
		return o1.equals(o2);
	}

	public static boolean equalsIgnoreCase(String s1, String s2) {
		if (s1 == null && s2 == null) {
			return true;
		}
		if (s1 == null || s2 == null) {
			return false;
		}
		return s1.equalsIgnoreCase(s2);
	}

	public static Account readAccount(String s) {
		try {
			JSONObject o = new JSONObject(s);
			Account account = new Account();
			if (o.has("id")) {
				account.setId(o.getLong("id"));
			}
			if (o.has("version")) {
				account.setVersion(o.getLong("version"));
			}
			if (o.has("name")) {
				account.setName(o.getString("name"));
			}
			return account;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static Device readDevice(String s) {
		try {
			JSONObject o = new JSONObject(s);
			Device device = new Device();
			if (o.has("id")) {
				device.setId(o.getLong("id"));
			}
			if (o.has("version")) {
				device.setVersion(o.getLong("version"));
			}
			if (o.has("code1")) {
				device.setCode1(o.getString("code1"));
			}
			if (o.has("code2")) {
				device.setCode2(o.getString("code2"));
			}
			if (o.has("description")) {
				device.setDescription(o.getString("description"));
			}
			return device;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static DeviceConfiguration readDeviceConfiguration(String s) {
		try {
			JSONObject o = new JSONObject(s);
			DeviceConfiguration deviceConfiguration = new DeviceConfiguration();
			if (o.has("id")) {
				deviceConfiguration.setId(o.getLong("id"));
			}
			if (o.has("version")) {
				deviceConfiguration.setVersion(o.getLong("version"));
			}
			if (o.has("delay1")) {
				deviceConfiguration.setDelay1(o.getLong("delay1"));
			}
			if (o.has("delay2")) {
				deviceConfiguration.setDelay2(o.getLong("delay2"));
			}
			if (o.has("delay3")) {
				deviceConfiguration.setDelay3(o.getLong("delay3"));
			}
			return deviceConfiguration;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static Location readLocation(String s) {
		try {
			JSONObject o = new JSONObject(s);
			Location location = new Location();
			if (o.has("id")) {
				location.setId(o.getLong("id"));
			}
			if (o.has("version")) {
				location.setVersion(o.getLong("version"));
			}
			if (o.has("accuracy")) {
				location.setAccuracy(o.getDouble("accuracy"));
			}
			if (o.has("altitude")) {
				location.setAltitude(o.getDouble("altitude"));
			}
			if (o.has("bearing")) {
				location.setBearing(o.getDouble("bearing"));
			}
			if (o.has("latitude")) {
				location.setLatitude(o.getDouble("latitude"));
			}
			if (o.has("longitude")) {
				location.setLongitude(o.getDouble("longitude"));
			}
			if (o.has("provider")) {
				location.setProvider(o.getString("provider"));
			}
			if (o.has("run")) {
				location.setRun(o.getLong("run"));
			}
			if (o.has("speed")) {
				location.setSpeed(o.getDouble("speed"));
			}
			if (o.has("time")) {
				location.setTime(new Date(o.getLong("time")));
			}
			return location;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String readString(InputStream is) {
		try {
			InputStreamReader isr = new InputStreamReader(is);
			char[] cs = new char[1 * 1024];
			StringBuffer sb = new StringBuffer();
			while (true) {
				int i = isr.read(cs);
				if (i == -1) {
					break;
				}
				sb.append(cs, 0, i);
			}
			isr.close();
			return sb.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static User readUser(String s) {
		try {
			JSONObject o = new JSONObject(s);
			User user = new User();
			if (o.has("id")) {
				user.setId(o.getLong("id"));
			}
			if (o.has("key")) {
				user.setKey(o.getString("key"));
			}
			if (o.has("version")) {
				user.setVersion(o.getLong("version"));
			}
			return user;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String writeAccount(Account account) {
		try {
			JSONObject o = new JSONObject();
			if (account.getId() != null) {
				o.put("id", account.getId());
			}
			if (account.getVersion() != null) {
				o.put("version", account.getVersion());
			}
			if (account.getName() != null) {
				o.put("name", account.getName());
			}
			return o.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String writeDevice(Device device) {
		try {
			JSONObject o = new JSONObject();
			if (device.getId() != null) {
				o.put("id", device.getId());
			}
			if (device.getVersion() != null) {
				o.put("version", device.getVersion());
			}
			if (device.getCode1() != null) {
				o.put("code1", device.getCode1());
			}
			if (device.getCode2() != null) {
				o.put("code2", device.getCode2());
			}
			if (device.getDescription() != null) {
				o.put("description", device.getDescription());
			}
			return o.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String writeDeviceConfiguration(
			DeviceConfiguration deviceConfiguration) {
		try {
			JSONObject o = new JSONObject();
			if (deviceConfiguration.getId() != null) {
				o.put("id", deviceConfiguration.getId());
			}
			if (deviceConfiguration.getVersion() != null) {
				o.put("version", deviceConfiguration.getVersion());
			}
			if (deviceConfiguration.getDelay1() != null) {
				o.put("delay1", deviceConfiguration.getDelay1());
			}
			if (deviceConfiguration.getDelay2() != null) {
				o.put("delay2", deviceConfiguration.getDelay2());
			}
			if (deviceConfiguration.getDelay3() != null) {
				o.put("delay3", deviceConfiguration.getDelay3());
			}
			return o.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String writeLocation(Location location) {
		try {
			JSONObject o = new JSONObject();
			if (location.getId() != null) {
				o.put("id", location.getId());
			}
			if (location.getVersion() != null) {
				o.put("version", location.getVersion());
			}
			if (location.getAccuracy() != null) {
				o.put("accuracy", location.getAccuracy());
			}
			if (location.getAltitude() != null) {
				o.put("altitude", location.getAltitude());
			}
			if (location.getBearing() != null) {
				o.put("bearing", location.getBearing());
			}
			if (location.getLatitude() != null) {
				o.put("latitude", location.getLatitude());
			}
			if (location.getLongitude() != null) {
				o.put("longitude", location.getLongitude());
			}
			if (location.getProvider() != null) {
				o.put("provider", location.getProvider());
			}
			if (location.getRun() != null) {
				o.put("run", location.getRun());
			}
			if (location.getSpeed() != null) {
				o.put("speed", location.getSpeed());
			}
			if (location.getTime() != null) {
				o.put("time", location.getTime().getTime());
			}
			return o.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void writeString(OutputStream os, String s) {
		try {
			os.write(s.getBytes());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String writeUser(User user) {
		try {
			JSONObject o = new JSONObject();
			if (user.getId() != null) {
				o.put("id", user.getId());
			}
			if (user.getKey() != null) {
				o.put("key", user.getKey());
			}
			if (user.getVersion() != null) {
				o.put("version", user.getVersion());
			}
			return o.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
